package vn.axonactive.rest;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

import vn.axonactive.Product;
import vn.axonactive.utils.ProductFactory;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

/**
 * @author nhan.nguyen
 */
@Path("/products")
public class ProductService {

	/**
	 * Search product by title and ranking Url:
	 * http://localhost:8080/RESTfulExample/rest/products/query?title=abc&rank=2
	 * 
	 * @param title
	 * @param rank
	 * @return list Product
	 */

	@GET
	@Path("/query")
	@Produces(MediaType.APPLICATION_XML)
	public List<Product> searchProducts(@QueryParam("title") String title,
			@QueryParam("rank") long rank) {
		List<Product> products = new ArrayList<Product>();

		products
				.addAll(ProductFactory.getInstance().searchProduct(title, rank));
		return products;

	}

	/**
	 * Search all products Url:
	 * http://localhost:8080/RESTfulExample/rest/products/
	 * 
	 * @return list Product
	 */

	@GET
	@Produces(MediaType.APPLICATION_XML)
	public List<Product> getProducts() {
		List<Product> products = new ArrayList<Product>();
		try {
			products.addAll(ProductFactory.getInstance().feedData());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return products;
	}

	/**
	 * Get product by code Url:
	 * http://localhost:8080/RESTfulExample/rest/products/ABC
	 * 
	 * @return product
	 */

	@GET
	@Path("{code}")
	public Product getProductByCode(@PathParam("code") String code) {

		return ProductFactory.getInstance().getProductByCode(code);

	}

	/**
	 * create Product by HTTP post
	 * 
	 * 
	 * @return messeage
	 */

	@POST
	@Produces(MediaType.TEXT_HTML)
	public String addProduct(@FormParam("code") String code,
			@FormParam("title") String title,
			@FormParam("description") String description,
			@FormParam("price") double price, @FormParam("rank") long rank,
			@FormParam("amount") long amount,
			@FormParam("imageURL") String imageURL) {

		Product product = new Product();
		product.setCode(code);
		product.setTitle(title);
		product.setDescription(description);
		product.setPrice(price);
		product.setRank(rank);
		product.setAmount(amount);
		product.setImageURL(imageURL);

		try {
			ProductFactory.getInstance().createProduct(product);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "Add Product successfully, title : " + title + ", price : "
				+ price;

	}

	/**
	 * create/update Product by HTTP PUT URL:
	 * http://localhost:8080/RESTfulExample/rest/products/ABC
	 * 
	 * @return messeage
	 */
	@PUT
	@Path("/{code}")
	@Consumes(MediaType.APPLICATION_XML)
	public String addUpdateProduct(@PathParam("code") String code, String body) {

		try {
			DOMParser parser = new DOMParser();
			parser.parse(new InputSource(new StringReader(body)));
			Document doc = parser.getDocument();
			Node node = doc.getElementsByTagName("Product").item(0);
			String title = node.getTextContent();
			Product product = new Product();
			product.setCode(code);
			product.setTitle(title);
			ProductFactory.getInstance().createUpdateProduct(product);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "Update/Create successfully";
	}

	/**
	 * create/update Product by HTTP DELETE URL:
	 * http://localhost:8080/RESTfulExample/rest/products/ABC
	 * 
	 * @return messeage
	 */

	@DELETE
	@Path("/{code}")
	public void removeProduct(@PathParam("code") String code) {
		try {
			ProductFactory.getInstance().removeProduct(code);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}